package com.spc.car.data.service.org.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.data.entity.org.OrgEmployee;
import com.spc.car.data.mapper.org.OrgEmployeeMapper;
import com.spc.car.data.domain.org.po.GetEmployeeListPO;
import com.spc.car.data.domain.org.po.SaveOrgEmployeePO;
import com.spc.car.data.service.org.OrgEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.data.domain.org.vo.OrgEmployeeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 组织雇员表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-30
 */
@Service
public class OrgEmployeeServiceImpl extends ServiceImpl<OrgEmployeeMapper, OrgEmployee> implements OrgEmployeeService {

    @Override
    public int saveEmployeeInformation(SaveOrgEmployeePO saveOrgEmployeePO) {
        OrgEmployee orgEmployee = new OrgEmployee();
        BeanUtils.copyProperties(saveOrgEmployeePO,orgEmployee);
        if (null != saveOrgEmployeePO.getId() && 0 < saveOrgEmployeePO.getId()){
            orgEmployee.setModifyTime(new Date());
            if (saveOrUpdate(orgEmployee)){
                return 1;
            }
            return 0;
        }else {
            orgEmployee.setCreateTime(new Date());
            if (saveOrUpdate(orgEmployee)){
                return 1;
            }
            return 0;
        }
    }

    @Override
    public boolean uniqueEmployee(SaveOrgEmployeePO saveOrgEmployeePO) {
        OrgEmployee info = getOne(new QueryWrapper<OrgEmployee>()
                .eq("org_code", saveOrgEmployeePO.getOrgCode())
                .eq("person_code", saveOrgEmployeePO.getPersonCode())
                .ne((null != saveOrgEmployeePO.getId() && 0 < saveOrgEmployeePO.getId()), "id", saveOrgEmployeePO.getId())
        );
        if (ObjectUtils.isNotNull(info)){
            return false;
        }
        return true;
    }

    @Override
    public Page<OrgEmployee> getEmployeeList(GetEmployeeListPO getEmployeeListPO) {
        Page page = page(getEmployeeListPO.getPage(), new QueryWrapper<OrgEmployee>()
                .eq(null != getEmployeeListPO.getId(), "id", getEmployeeListPO.getId())
                .eq(null != getEmployeeListPO.getInService(), "in_service", getEmployeeListPO.getInService())
                .like(StringUtils.isNotBlank(getEmployeeListPO.getOrgCode()), "org_code", getEmployeeListPO.getOrgCode())
                .like(StringUtils.isNotBlank(getEmployeeListPO.getOrgName()), "org_name", getEmployeeListPO.getOrgName())
                .like(StringUtils.isNotBlank(getEmployeeListPO.getPersonCode()), "person_code", getEmployeeListPO.getPersonCode())
                .like(StringUtils.isNotBlank(getEmployeeListPO.getPersonName()), "person_name", getEmployeeListPO.getPersonName())
        );
        if (page.getRecords().isEmpty()){
            return page;
        }

        List<OrgEmployeeListVO> orgEmployeeListVOS= new ArrayList<>();
        page.getRecords().stream().forEach(anEmployee ->{
            OrgEmployeeListVO orgEmployeeListVO = new OrgEmployeeListVO();
            BeanUtils.copyProperties(anEmployee, orgEmployeeListVO);
            orgEmployeeListVOS.add(orgEmployeeListVO);
        });

        return page.setRecords(orgEmployeeListVOS);
    }

}
